package com.yanghongyue.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yanghongyue.reggie.bean.Category;
import com.yanghongyue.reggie.bean.Setmeal;
import com.yanghongyue.reggie.bean.SetmealDish;
import com.yanghongyue.reggie.bean.dto.SetmealDto;
import com.yanghongyue.reggie.common.CustException;
import com.yanghongyue.reggie.common.R;
import com.yanghongyue.reggie.service.CategoryService;
import com.yanghongyue.reggie.service.SetmealDishService;
import com.yanghongyue.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author HongyueYang
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;

    //分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        //创建分页对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        //创建条件对象
        QueryWrapper wrapper = new QueryWrapper<>();
        if (name != null && !name.equals("")) {
            wrapper.like("name", name);
        }
        //执行分页查询
        setmealService.page(pageInfo, wrapper);

        //创建dto对象
        Page<Setmeal> dtoPage = new Page<>();
        //复制分页对象的属性值(排除数据集合)
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");

        //获取数据集合
        List<Setmeal> setmealList = pageInfo.getRecords();
        //创建dto数据集合
        List<Setmeal> dtoList = new ArrayList<>();

        //遍历数据集合
        for (Setmeal setmeal : setmealList) {
            //创建dto对象
            SetmealDto setmealDto = new SetmealDto();
            //复制属性值
            BeanUtils.copyProperties(setmeal, setmealDto);
            //根据套餐对象中的分类id，查询分类对象
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null) {
                //给dto设置分类对象
                setmealDto.setCategoryName(category.getName());
            }
            //将dto对象添加到dto数据集合中
            dtoList.add(setmealDto);
        }

        //给dto的集合设置值
        dtoPage.setRecords(dtoList);
        //返回结果
        return R.sucess(dtoPage);


    }

    //保存新增套餐
    @CacheEvict(value = "setmealCache", allEntries = true)
    @PostMapping
    public R<String> saveSetmealWithDish(@RequestBody SetmealDto dto) {
        setmealService.saveSetmealWithDish(dto);
        return R.sucess("新增套餐成功");
    }

    //根据Id获取套餐
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable("id") Long id) {
        return R.sucess(setmealService.getSetmealWithDish(id));
    }

    //修改套餐
    @CacheEvict(value = "setmealCache", allEntries = true)
    @PutMapping
    public R<String> updateSetmealWithDish(@RequestBody SetmealDto dto) {
        setmealService.updateSetmealWithDish(dto);
        return R.sucess("修改套餐成功");
    }

    //修改状态
    @CacheEvict(value = "setmealCache", allEntries = true)
    @PostMapping("/status/{status}")
    public R<String> updatestatus(@PathVariable("status") Integer status, @RequestParam List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.sucess("修改状态成功");
    }

    //    //根据状态删除菜品
//    @DeleteMapping
//    public R<String> delete(@RequestParam Long[] ids) {
//        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
//        wrapper.in("setmeal_id", ids);
//
//
//        List<Long> list = Arrays.asList(ids);
//        for (Long id : list) {
//            if (setmealService.getById(id).getStatus() == 1) {
//                return R.error("套餐正在售卖中，不能删除");
//            } else {
//                setmealService.removeById(id);
//                setmealDishService.remove(wrapper);
//            }
//        }
//        return R.sucess("删除成功");
//    }
    //根据状态删除套餐
    @CacheEvict(value = "setmealCache", allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam Long[] ids) throws CustException {
        setmealService.deleteSetmealWithDish(ids);
        return R.sucess("删除成功");
    }

    //根据分类id查询套餐
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' +#setmeal.status")
    @GetMapping("/list")
    public R<List> list(Setmeal setmeal) {

        //根据分类id查询套餐
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", setmeal.getCategoryId());
        wrapper.eq("status", 1);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.sucess(setmealList);
    }

    //根据套餐id查询关联的菜品关系数据
    @GetMapping("/dish/{id}")
    public R<List> getDishListById(@PathVariable("id") Long id) {
        //根据套餐id查询菜品关系数据
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", id);
        List<SetmealDish> setmealDishList = setmealDishService.list(wrapper);
        return R.sucess(setmealDishList);

    }


}